<?php
	class Response
	{
		
		const RETURN_TYPE_XML = 'xml';
		const RETURN_TYPE_JSON = 'json';
		
		
		public static function SUCCESS( $resultObject, $returnType='xml' )
		{
			if( strtolower( $returnType ) == Response::RETURN_TYPE_XML )
			{
				$response = '<response>';
				$response .= '		<result>SUCCESS</result>';
				
				$response .= self::encodeToXMLNode( $resultObject );
				
				$response .= '</response>';
				return $response;
				
			}else if( strtolower( $returnType ) == Response::RETURN_TYPE_JSON )
			{
				$resultObject[ 'result' ] = 'SUCCESS';
				return json_encode( $resultObject );
			}
		}
		
		private static function encodeToXMLNode( $obj, $pk='' )
		{
			if( is_bool( $obj ) ) return $obj;
			if( is_numeric( $obj ) ) return $obj;
			if( is_string( $obj ) ) return $obj;
			
			$rtn = '';
			foreach( $obj as $k=>$v )
			{
				if( is_numeric( $k ) )
				{
					$rtn .= ("<$pk>".self::encodeToXMLNode($v,$pk)."</$pk>");
				}else if( is_array( $v ) )
				{
					foreach( $v as $k2=>$v2 )
					{
						$rtn .= ("<$k>".self::encodeToXMLNode($v2, $k2)."</$k>");
					}
				}else {
					$rtn .= ("<$k>".self::encodeToXMLNode($v)."</$k>");
				}
			}
			return $rtn;
		}
		
		
		
		public static function FAIL( $code, $message, $returnType='xml' )
		{
			if( strtolower( $returnType ) == Response::RETURN_TYPE_XML )
			{
				return '<response>'
						.'		<result>FAIL</result>'
						.'		<code>'.$code.'</code>'
						.'		<message>'.$message.'</message>'
						.'</response>';
			}else if( strtolower( $returnType ) == Response::RETURN_TYPE_JSON )
			{
				$resultObject = array();
				$resultObject[ 'result' ] 	= 'FAIL';
				$resultObject[ 'code' ] 	= $code;
				$resultObject[ 'message' ] 	= $message;;
				
				return json_encode( $resultObject );
			}
		}
	}
?>